package com.yupi.springbootinit.manager;

import com.yupi.springbootinit.model.enums.StringEnum;
import io.github.briqt.spark4j.SparkClient;
import io.github.briqt.spark4j.constant.SparkApiVersion;
import io.github.briqt.spark4j.exception.SparkException;
import io.github.briqt.spark4j.model.SparkMessage;
import io.github.briqt.spark4j.model.SparkSyncChatResponse;
import io.github.briqt.spark4j.model.request.SparkRequest;
import io.github.briqt.spark4j.model.response.SparkTextUsage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ChartManager {
    @Resource
    private SparkClient sparkClient;

    public String doChat(String message) {
// 消息列表，可以在此列表添加历史对话记录
        List<SparkMessage> messages = new ArrayList<>();
        messages.add(SparkMessage.systemContent(StringEnum.chartFront));
        messages.add(SparkMessage.userContent(message));
// 构造请求
        SparkRequest sparkRequest = SparkRequest.builder()
// 消息列表
                .messages(messages)
                .maxTokens(2048)
                .temperature(0.2)
// 指定请求版本，默认使用最新3.0版本
                .apiVersion(SparkApiVersion.V3_5)
                .build();
        String content = null;
        try {
            // 同步调用
            SparkSyncChatResponse chatResponse = sparkClient.chatSync(sparkRequest);
            SparkTextUsage textUsage = chatResponse.getTextUsage();
            content = chatResponse.getContent();

            System.out.println("\n回答：" + content);
            System.out.println("\n提问tokens：" + textUsage.getPromptTokens()
                    + "，回答tokens：" + textUsage.getCompletionTokens()
                    + "，总消耗tokens：" + textUsage.getTotalTokens());
        } catch (SparkException e) {
            log.error("发生异常了：" + e.getMessage());
        }
        if (content != null) {
            content = content.replace("json", "");
            content = content.replace("```", "");
            content = content.trim();
        }
        return content;
    }

    public String doAnalys(String message, Map<String, String> answerByAi) {
// 消息列表，可以在此列表添加历史对话记录
        List<SparkMessage> messages = new ArrayList<>();
        messages.add(SparkMessage.systemContent(StringEnum.analyseFront));
        messages.add(SparkMessage.userContent(answerByAi.get("提问")));
        messages.add(SparkMessage.assistantContent(answerByAi.get("回答")));
        messages.add(SparkMessage.userContent("根据以上给出的数据帮我分析用户数量增长趋势"));
        messages.add(SparkMessage.assistantContent(StringEnum.standAnswer));
        messages.add(SparkMessage.userContent("现请你根据以上案例的格式再详细" + message+"不要任何代码，只需要分析结果"));
// 构造请求
        SparkRequest sparkRequest = SparkRequest.builder()
// 消息列表
                .messages(messages)
                .maxTokens(2048)
                .temperature(0.2)
// 指定请求版本，默认使用最新3.0版本
                .apiVersion(SparkApiVersion.V3_5)
                .build();
        String content = null;
        try {
            // 同步调用
            SparkSyncChatResponse chatResponse = sparkClient.chatSync(sparkRequest);
            SparkTextUsage textUsage = chatResponse.getTextUsage();
            content = chatResponse.getContent();

            System.out.println("\n回答：" + content);
            System.out.println("\n提问tokens：" + textUsage.getPromptTokens()
                    + "，回答tokens：" + textUsage.getCompletionTokens()
                    + "，总消耗tokens：" + textUsage.getTotalTokens());
        } catch (SparkException e) {
            log.error("发生异常了：" + e.getMessage());
        }
        return content;
    }
}
